package servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Customer;
import model.Offer;
import model.Trade;

import org.w3c.dom.Document;

import persistence.CustomerDao;
import persistence.TradeDao;

/**
 * Servlet implementation class PersonContent
 */
@WebServlet("/PersonContent")
public class PersonContent extends MyHttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request,response);
	}
	
	//TODO Connect the Database & Remove temp implement
	private void process(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Document xmlDoc = getDocument(request);
			String username = xmlDoc.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();
			
			
			response.setContentType("application/xml");
			response.setCharacterEncoding("UTF-8");
			printWriter = response.getWriter();
			printWriter.write("<content>");
			
//			//TEMP IMPLEMENT
//			printWriter.write("<user>");
//			addTag("name","etenaelta");
//			addTag("description","I'm sausage.I live in the fridge. I am covered with frost and waiting for my expiration time");
//			addTag("email","sausage@gmail.com");
//			addTag("phone","13832165887");
//			addTag("gender", "男");
//			addTag("image", "userimages\\8919982331744603747.jpg");
//			printWriter.write("</user>");
//			
//			printWriter.write("<entries>");
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-04");
//			addTag("title", "Linear Algebra 1000 Exercise");
//			addTag("author", "etenaelta");
//			addTag("image", "");
//			addTag("type", "myDifficulty");
//			
//			addTag("status", "status_yes");//status_yes --- not completed	status_no --- completed
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896322");
//			addTag("date", "2013-02-04");
//			addTag("title", "A Life With Linear Algebra 1000 Exercise");
//			addTag("author", "gccc");
//			addTag("image", "");
//			addTag("type", "myJoin");
//			
//			addTag("status", "status_yes");//status_yes --- not completed	status_no --- completed
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-04");
//			addTag("title", "Linear Algebra 1000 Exercises");
//			addTag("author", "etenaelta");
//			addTag("image", "");
//			addTag("type", "myLove");
//			
//			addTag("status", "status_yes");//status_yes --- not completed	status_no --- completed
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("</entries>");
//			//TEMP IMPLEMENT END
			
			Customer customer = CustomerDao.load(CustomerDao.nameToID(username));
			
			
			printWriter.write("<user>");
			addTag("name",username);
			addTag("description",customer.getDescription());
			addTag("email",customer.getEmail());
			addTag("phone",customer.getPhone());
			addTag("gender", customer.getGender()=='m' ? "男" : "女");
			addTag("image",customer.getStatus());
			
			printWriter.write("</user>");
			
			
			printWriter.write("<entries>");
			
			
			@SuppressWarnings("unchecked")
			Set<Trade> tradeSet = customer.getTrade();
			
			Iterator<Trade> it = tradeSet.iterator();
			
			while(it.hasNext()) {
				printWriter.write("<entry>");
				Trade buffer = it.next();
				addTag("id", String.valueOf(buffer.getId()));
				addTag("date", buffer.getDate().toString());
				addTag("title", buffer.getTitle());
				addTag("author", buffer.getOwner().getName());
				addTag("image",buffer.getOwner().getStatus());
				addTag("type", "myDifficulty");
				
				//change to real function later
				addTag("status", TradeDao.isFinish(buffer.getId()) ? "status_no" : "status_yes");//status_yes --- not completed	status_no --- completed
				addTag("collect", TradeDao.isFavorite(CustomerDao.nameToID(username),buffer.getId()) ? "collectButton_yes" : "collectButton_no");
				printWriter.write("</entry>");
			}
			
			@SuppressWarnings("unchecked")
			Set<Offer> sellSet = customer.getSell();
			
			Iterator<Offer> offer_it = sellSet.iterator();
			
			while(offer_it.hasNext()) {
				printWriter.write("<entry>");
				Offer offer_buffer = offer_it.next();
				Trade buffer = offer_buffer.getTrade();
				addTag("id", String.valueOf(buffer.getId()));
				addTag("date", buffer.getDate().toString());
				addTag("title", buffer.getTitle());
				addTag("author", buffer.getOwner().getName());
				addTag("image",buffer.getOwner().getStatus());
				addTag("type", "myJoin");
				
				//change to real function later
				addTag("status", TradeDao.isFinish(buffer.getId()) ? "status_no" : "status_yes");//status_yes --- not completed	status_no --- completed
				addTag("collect", TradeDao.isFavorite(CustomerDao.nameToID(username),buffer.getId()) ? "collectButton_yes" : "collectButton_no");
				printWriter.write("</entry>");
			}
			
			@SuppressWarnings("unchecked")
			Set<Trade> favouriteSet = customer.getFavorite();
			
			it = favouriteSet.iterator();
			
			while(it.hasNext()) {
				printWriter.write("<entry>");
				Trade buffer = it.next();
				addTag("id", String.valueOf(buffer.getId()));
				addTag("date", buffer.getDate().toString());
				addTag("title", buffer.getTitle());
				addTag("author", buffer.getOwner().getName());
				addTag("image",buffer.getOwner().getStatus());
				addTag("type", "myLove");
				
				//change to real function later
				addTag("status", TradeDao.isFinish(buffer.getId()) ? "status_no" : "status_yes");//status_yes --- not completed	status_no --- completed
				addTag("collect", TradeDao.isFavorite(CustomerDao.nameToID(username),buffer.getId()) ? "collectButton_yes" : "collectButton_no");
				printWriter.write("</entry>");
			}
			
			printWriter.write("</entries>");
			printWriter.write("</content>");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
